package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.ToolKit;
import com.ruoyi.system.mapper.ToolKitMapper;
import com.ruoyi.system.service.ToolKitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author ：Wangziyi
 * @date ：Created in 2023/12/17 17:30
 * @description：ToDo
 * @modified By：
 * @version: 1.0
 */
@Service
public class ToolKitServiceImpl extends ServiceImpl<ToolKitMapper, ToolKit> implements ToolKitService {
    @Autowired
    private ToolKitMapper toolKitMapper;

    @Override
    public ToolKit getToolkitByTkName(String intention) {
        LambdaQueryWrapper<ToolKit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ToolKit::getTkName,intention).eq(ToolKit::getIsDeleted,0);
        List<ToolKit> list = this.list(queryWrapper);
        if (list == null && list.size() == 0){
            throw new ServiceException("找不到对应工具集");
        }
        return list.get(0);
    }

    @Override
    public ToolKit getToolkitByName(String tkName) {
        LambdaQueryWrapper<ToolKit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ToolKit::getTkName,tkName).eq(ToolKit::getIsDeleted,0);
        List<ToolKit> list = this.list(queryWrapper);
        if (list == null && list.size() == 0){
            throw new ServiceException("找不到对应工具集");
        }
        return list.get(0);
    }

    @Override
    public boolean updateToolKitPromptByTkName(String tkName ,String tkPrompt){
        LambdaUpdateWrapper<ToolKit> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ToolKit::getTkName,tkName).eq(ToolKit::getIsDeleted,0);
        updateWrapper.set(ToolKit::getTkPrompt,tkPrompt);
        updateWrapper.set(ToolKit::getGmtModified, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return this.update(updateWrapper);
    }

    @Override
    public List<ToolKit> getAllTookit() {
        LambdaQueryWrapper<ToolKit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ToolKit::getIsDeleted,0);
        List<ToolKit> list = this.list(queryWrapper);
        if (list == null && list.size() == 0){
            throw new ServiceException("没有工具集");
        }
        return list;
    }


    @Override
    public Page<ToolKit> selectTookit(Page<ToolKit> page,  Wrapper<ToolKit> queryWrapper) {
        return toolKitMapper.selectTookit(page, queryWrapper);
    }
}
